home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / src / load.ld.c < prev    next >
C/C++ Source or Header  |  1992-10-16  |  3KB  |  143 lines

  1. #include <a.out.h>
  2. #include <sys/types.h>
  3.  
  4. #ifdef ECOFF
  5. #  ifdef CACHECTL_H
  6. #    include CACHECTL_H
  7. #  endif
  8.  
  9. struct headers {
  10.     struct filehdr fhdr;
  11.     struct aouthdr aout;
  12.     struct scnhdr section[3];    
  13. };
  14. #endif
  15.  
  16. extern char *sbrk();
  17.  
  18. static char Loader_Output[20];
  19.  
  20. Load_Object (names) Object names; {
  21. #ifdef ECOFF
  22.     struct headers hdr;
  23. #else
  24.     struct exec hdr;
  25. #endif
  26.     register char *brk, *obrk, *lp, *li;
  27.     char *buf;
  28.     register n, f, len, liblen;
  29.     Object port, tail, fullnames, libs;
  30.     FILE *fp;
  31.     extern char *mktemp();
  32.     GC_Node3;
  33.     Alloca_Begin;
  34.  
  35.     li = Loader_Input;
  36.     if (li[0] == 0)
  37.     li = A_Out_Name;
  38.     strcpy (Loader_Output, "/tmp/ldXXXXXX");
  39.     (void)mktemp (Loader_Output);
  40.  
  41.     port = tail = fullnames = Null;
  42.     GC_Link3 (port, tail, fullnames);
  43.     for (len = 0, tail = names; !Nullp (tail); tail = Cdr (tail)) {
  44.     port = General_Open_File (Car (tail), P_INPUT, Var_Get (V_Load_Path));
  45.     fullnames = Cons (PORT(port)->name, fullnames);
  46.     len += STRING(Car (fullnames))->size + 1;
  47.     (void)P_Close_Input_Port (port);
  48.     }
  49.     GC_Unlink;
  50.  
  51.     libs = Var_Get (V_Load_Libraries);
  52.     if (TYPE(libs) == T_String) {
  53.         liblen = STRING(libs)->size;
  54.     lp = STRING(libs)->data;
  55.     } else {
  56.     liblen = 3; lp = "-lc";
  57.     }
  58.  
  59.     Alloca (buf, char*, strlen (A_Out_Name) + len + liblen + 100);
  60.  
  61.     obrk = brk = sbrk (0);
  62.     brk = (char *)((int)brk + 7 & ~7);
  63.  
  64. #if defined(hp9000s300) || defined(__hp9000s300__)
  65.     sprintf (buf, "ld -N%s -A %s -R %x -o %s ",
  66. #else
  67.     sprintf (buf, "ld -N%s -A %s -T %x -o %s ",
  68. #endif
  69. #ifdef USE_XFLAG
  70.     " -x",
  71. #else
  72.     "",
  73. #endif
  74.     li, (unsigned)brk, Loader_Output);
  75.  
  76.     for (tail = fullnames; !Nullp (tail); tail = Cdr (tail)) {
  77.     register struct S_String *str = STRING(Car (tail));
  78.     strncat (buf, str->data, str->size);
  79.     strcat (buf, " ");
  80.     }
  81.     strncat (buf, lp, liblen);
  82.  
  83.     if (Verbose)
  84.     printf ("[%s]\n", buf);
  85.     if (system (buf) != 0) {
  86.     (void)unlink (Loader_Output);
  87.     Primitive_Error ("system linker failed");
  88.     }
  89.     Disable_Interrupts;               /* To ensure that f gets closed */
  90.     if ((f = open (Loader_Output, 0)) == -1) {
  91.     (void)unlink (Loader_Output);
  92.     Primitive_Error ("cannot open tempfile");
  93.     }
  94.     if (Loader_Input[0])
  95.     (void)unlink(Loader_Input);
  96.     strcpy (Loader_Input, Loader_Output);
  97.     if (read (f, (char *)&hdr, sizeof (hdr)) != sizeof (hdr)) {
  98. err:
  99.     close (f);
  100.     Primitive_Error ("corrupt tempfile (`ld' is broken)");
  101.     }
  102. #ifdef ECOFF
  103.     n = hdr.aout.tsize + hdr.aout.dsize + hdr.aout.bsize;
  104. #else
  105.     n = hdr.a_text + hdr.a_data + hdr.a_bss;
  106. #endif
  107.     n += brk - obrk;
  108.     if (sbrk (n) == (char *)-1) {
  109.     close (f);
  110.     Primitive_Error ("not enough memory to load object file");
  111.     }
  112.     bzero (obrk, n);
  113. #ifdef ECOFF
  114.     n -= hdr.aout.bsize;
  115.     (void)lseek (f, (long)hdr.section[0].s_scnptr, 0);
  116. #else
  117.     n -= hdr.a_bss;
  118. #endif
  119.     if (read (f, brk, n) != n)
  120.     goto err;
  121.     if ((fp = fdopen (f, "r")) == NULL) {
  122.     close (f);
  123.     Primitive_Error ("cannot fdopen object file");
  124.     }
  125.     if (The_Symbols)
  126.     Free_Symbols (The_Symbols);
  127.     The_Symbols = Snarf_Symbols (fp, &hdr);
  128.     (void)fclose (fp);
  129. #if defined(ECOFF) && defined(CACHECTL_H)
  130.     if (cacheflush (brk, n, BCACHE) < 0)
  131.     Primitive_Error ("cacheflush failed: ~E");
  132. #endif
  133.     if (!Call_Initializers (The_Symbols, brk))
  134.     Primitive_Error ("no initializers in object file(s)");
  135.     Enable_Interrupts;
  136.     Alloca_End;
  137. }
  138.  
  139. Finit_Load () {
  140.     if (Loader_Input[0])
  141.     (void)unlink (Loader_Input);
  142. }
  143.